<template>
  <div class="app-container">
    <h2 class="data-list-title" style=" margin-bottom: 5px; color: #6495ED; font-family: 'Roboto', 'Arial Narrow', sans-serif; font-weight: 500;">
      <i class="el-icon-search"></i>
        租赁合同信息
    </h2>
    <div style="border-bottom: 2px solid #6495ED;margin-top: 0px;margin-bottom: 10px;"></div>
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['agtmanaview:agtmanaview:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['agtmanaview:agtmanaview:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['agtmanaview:agtmanaview:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['agtmanaview:agtmanaview:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="agtmanaviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租赁合同ID" align="center" prop="leaseAgreementId" width="90"/>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180"/>
      <el-table-column label="租赁时间/月" align="center" prop="leaseTerm" width="110"/>
      <el-table-column label="总租金" align="center" prop="allRent" width="150">
        <template #default="scope">
          {{ scope.row.allRent !== null ? '¥' + scope.row.allRent : '' }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="agree_appoint_stutas" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="移动端用户ID" align="center" prop="mobileUserId" width="110"/>
      <el-table-column label="用户名" align="center" prop="name" />
      <el-table-column label="手机号码" align="center" prop="phoneNumber" width="110"/>
      <el-table-column label="公寓ID" align="center" prop="apartmentId" />
      <el-table-column label="公寓名称" align="center" prop="apartmentName" />
      <el-table-column label="房间ID" align="center" prop="roomId" />
      <el-table-column label="房间名称" align="center" prop="roomName" />
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="区/县" align="center" prop="county" />
      <el-table-column label="房间具体位置" align="center" prop="roomLocation" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['agtmanaview:agtmanaview:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['agtmanaview:agtmanaview:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <el-divider content-position="center" style="margin-top: 50px;"></el-divider>
    <h2 class="data-list-title" style=" margin-bottom: 5px;margin-top: 50px; color: #6495ED; font-family: 'Roboto', 'Arial Narrow', sans-serif; font-weight: 500;">
      <i class="el-icon-search"></i>
        搜索条件
    </h2>
    <div style="border-bottom: 2px solid #6495ED;margin-top: 0px;margin-bottom: 10px;"></div>
    
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="110px" label-position="left">
      <h3 style="color: #6495ED; font-family: 'Roboto', 'Arial Narrow', sans-serif; font-weight: 400;">
        租赁合同搜索条件：
      </h3>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="租赁合同ID:" prop="leaseAgreementId">
            <el-input
              v-model="queryParams.leaseAgreementId"
              placeholder="请输入租赁合同ID"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开始时间:" style="width: 100%;">
            <el-date-picker
              v-model="daterangeStartTime"
              value-format="YYYY-MM-DD HH:mm"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始日期时间"
              end-placeholder="结束日期时间"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="租赁时间/月:" prop="leaseTerm">
            <el-input
              v-model="queryParams.leaseTerm"
              placeholder="请输入租赁时间/月"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="总租金:" prop="allRent">
            <el-input
              v-model="queryParams.allRent"
              placeholder="请输入总租金"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="状态:" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in agree_appoint_stutas"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="center" style="margin: 0px 0;"></el-divider>
      <h3 style="color: #6495ED; font-family: 'Roboto', 'Arial Narrow', sans-serif; font-weight: 400;">
        用户信息搜索条件：
      </h3>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="移动端用户ID:" prop="mobileUserId">
            <el-input
              v-model="queryParams.mobileUserId"
              placeholder="请输入移动端用户ID"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用户名:" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入用户名"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="手机号码:" prop="phoneNumber">
            <el-input
              v-model="queryParams.phoneNumber"
              placeholder="请输入手机号码"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="center" style="margin: 0px 0;"></el-divider>
      <h3 style="color: #6495ED; font-family: 'Roboto', 'Arial Narrow', sans-serif; font-weight: 400;">
        公寓/房间搜索条件：
      </h3>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="公寓ID:" prop="apartmentId">
            <el-input
              v-model="queryParams.apartmentId"
              placeholder="请输入公寓ID"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="公寓名称:" prop="apartmentName">
            <el-input
              v-model="queryParams.apartmentName"
              placeholder="请输入公寓名称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="房间ID:" prop="roomId">
            <el-input
              v-model="queryParams.roomId"
              placeholder="请输入房间ID"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="房间名称:" prop="roomName">
            <el-input
              v-model="queryParams.roomName"
              placeholder="请输入房间名称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="省份" prop="province">
            <el-select v-model="queryParams.province" placeholder="请选择省份" clearable @change="changeCityListQuery($event)">
              <el-option
                v-for="province in provinceListQuery"
                :key="province.id"
                :label="province.name"
                :value="province.name"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="城市" prop="city">
            <el-select v-model="queryParams.city" placeholder="请选择城市" clearable @change="changeCountyListQuery($event)" :disabled="!queryParams.province">
              <el-option
                v-for="city in cityListQuery"
                :key="city.id"
                :label="city.name"
                :value="city.name"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="区/县" prop="county">
            <el-select v-model="queryParams.county" placeholder="请选择区/县" clearable :disabled="!queryParams.city">
              <el-option
                v-for="county in countyListQuery"
                :key="county.id"
                :label="county.name"
                :value="county.name"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="房间具体位置:" prop="roomLocation">
            <el-input
              v-model="queryParams.roomLocation"
              placeholder="请输入房间具体位置"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-divider content-position="center" style="margin-top: 50px;"></el-divider>
    
    
    

    <!-- 添加或修改租赁合同管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-divider content-position="center" style="margin-top: 0px;">租赁合同信息</el-divider>
      <el-form ref="agtmanaviewRef" :model="form" :rules="rules" label-width="110px" label-position="left">
        <el-form-item label="租赁合同ID" prop="leaseAgreementId" v-show="false">
          <el-input v-model="form.leaseAgreementId" placeholder="请输入租赁合同ID" />
        </el-form-item>
        <el-form-item label="合同详情" prop="agreementDetails"> 
          <el-input v-model="form.agreementDetails" placeholder="请输入合同详情" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="租赁时间/月" prop="leaseTerm">
          <el-input v-model="form.leaseTerm" placeholder="请输入租赁时间/月" />
        </el-form-item>
        <el-form-item label="总租金" prop="allRent">
          <el-input v-model="form.allRent" placeholder="请输入总租金" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in agree_appoint_stutas"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">对应关系</el-divider>
        <el-form-item label="移动端用户ID" prop="mobileUserId">
          <el-input v-model="form.mobileUserId" placeholder="请输入移动端用户ID" />
        </el-form-item>
        <el-form-item label="房间ID" prop="roomId">
          <el-input v-model="form.roomId" placeholder="请输入房间ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Agtmanaview">
import { listAgtmanaview, getAgtmanaview, delAgtmanaview, addAgtmanaview, updateAgtmanaview } from "@/api/agtmanaview/agtmanaview";

const { proxy } = getCurrentInstance();
const { agree_appoint_stutas } = proxy.useDict('agree_appoint_stutas');

const agtmanaviewList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeStartTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    leaseAgreementId: null,
    startTime: null,
    leaseTerm: null,
    allRent: null,
    status: null,
    mobileUserId: null,
    name: null,
    phoneNumber: null,
    apartmentId: null,
    apartmentName: null,
    roomId: null,
    roomName: null,
    province: null,
    city: null,
    county: null,
    roomLocation: null,
  },
  rules: {
    agreementDetails: [
      { required: true, message: "合同详情不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    leaseTerm: [
      { required: true, message: "租赁时间不能为空", trigger: "blur" },
      { validator: (rule, value, callback) => {
          if (value && !/^\d+$/.test(value)) {
            callback(new Error('租赁时间必须为数字'));
          } else {
            callback();
          }
        }, trigger: 'blur' 
      }
    ],
    allRent: [
      { required: true, message: "总租金不能为空", trigger: "blur" },
      { validator: (rule, value, callback) => {
          if (value && !/^\d+$/.test(value)) {
            callback(new Error('总租金必须为数字'));
          } else {
            callback();
          }
        }, trigger: 'blur' 
      }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
    mobileUserId: [
      { required: true, message: "移动端用户ID不能为空", trigger: "blur" },
      { validator: (rule, value, callback) => {
          if (value && !/^\d+$/.test(value)) {
            callback(new Error('移动端用户ID必须为数字'));
          } else {
            callback();
          }
        }, trigger: 'blur' 
      }
    ],
    roomId: [
      { required: true, message: "房间ID不能为空", trigger: "blur" },
      { validator: (rule, value, callback) => {
          if (value && !/^\d+$/.test(value)) {
            callback(new Error('房间ID必须为数字'));
          } else {
            callback();
          }
        }, trigger: 'blur' 
      }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询租赁合同管理列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeStartTime && '' != daterangeStartTime) {
    queryParams.value.params["beginStartTime"] = daterangeStartTime.value[0];
    queryParams.value.params["endStartTime"] = daterangeStartTime.value[1];
  }
  listAgtmanaview(queryParams.value).then(response => {
    agtmanaviewList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    leaseAgreementId: null,
    agreementDetails: null,
    startTime: null,
    leaseTerm: null,
    allRent: null,
    status: null,
    mobileUserId: null,
    name: null,
    phoneNumber: null,
    apartmentId: null,
    apartmentName: null,
    roomId: null,
    roomName: null,
    province: null,
    city: null,
    county: null,
    roomLocation: null,
    signId: null,
    agreementCorrespondRoomId: null
  };
  proxy.resetForm("agtmanaviewRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeStartTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.leaseAgreementId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加租赁合同管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _leaseAgreementId = row.leaseAgreementId || ids.value
  getAgtmanaview(_leaseAgreementId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改租赁合同管理";

    // 在获取数据后初始化城市列表和区县列表
    getCityListByName(form.value.province);
    getCountyListByName(form.value.city);
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["agtmanaviewRef"].validate(valid => {
    if (valid) {
      if (form.value.leaseAgreementId != null) {
        updateAgtmanaview(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAgtmanaview(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _leaseAgreementIds = row.leaseAgreementId || ids.value;
  proxy.$modal.confirm('是否确认删除租赁合同管理编号为"' + _leaseAgreementIds + '"的数据项？').then(function() {
    return delAgtmanaview(_leaseAgreementIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('agtmanaview/agtmanaview/export', {
    ...queryParams.value
  }, `agtmanaview_${new Date().getTime()}.xlsx`)
}

getList();

//----------------------------------------
//省市县功能实现：
import { listTopCity, cityListByName } from "@/api/district/district";

const provinceList = ref([]);
const cityList = ref([]);
const countyList = ref([]);
/*
增加修改对话框省市县功能实现：
*/
// 获取增加修改对话框省份列表
function getProvinceList() {
  listTopCity().then(response => {
    provinceList.value = response.rows;
  });
}

// 增加修改对话框根据省份名字获取城市列表
function getCityListByName(provinceName){
  cityListByName({name:provinceName}).then(response=>{
    cityList.value = response.rows;
  })
}

// 增加修改对话框根据城市名字获取区县列表
function getCountyListByName(cityName){
  cityListByName({name:cityName}).then(response=>{
    countyList.value = response.rows;
  })
}

// 增加修改对话框改变城市列表
function changeCityList(provinceName){
  form.value.city = null;
  form.value.county = null;
  getCityListByName(provinceName);
}

// 增加修改对话框改变区县列表
function changeCountyList(cityName){
  form.value.county = null;
  getCountyListByName(cityName);
}

// 初始化获取增加修改对话框省份列表
getProvinceList();
// 初始化获取增加修改对话框城市列表和区县列表
getCityListByName(form.value.province);
// 初始化获取增加修改对话框区县列表
getCountyListByName(form.value.city);

/*
搜索框省市县功能实现：
*/
const provinceListQuery = ref([]);
const cityListQuery = ref([]);
const countyListQuery = ref([]);
// 获取搜索框省份列表
function getProvinceQueryList() {
  listTopCity().then(response => {
    provinceListQuery.value = response.rows;
  });
}

// 搜索框根据省份名字获取城市列表
function getCityListQueryByName(provinceName){
  
  cityListByName({name:provinceName}).then(response=>{
    cityListQuery.value = response.rows;  
  })
}
// 搜索框根据城市名字获取区县列表
function getCountyListQueryByName(cityName){
  //打印cityName
  
  cityListByName({name:cityName}).then(response=>{
    countyListQuery.value = response.rows;
  })
}

// 改变搜索框城市列表
function changeCityListQuery(provinceName){
  queryParams.value.city = null;
  queryParams.value.county = null;
  getCityListQueryByName(provinceName);
}
// 改变搜索框区县列表
function changeCountyListQuery(cityName){
  queryParams.value.county = null;
  getCountyListQueryByName(cityName);
}
// 初始化获取搜索框省份列表
getProvinceQueryList();
// 初始化获取搜索框城市列表
getCityListQueryByName(queryParams.value.province);
// 初始化获取搜索框区县列表
getCountyListQueryByName(queryParams.value.city);

//----------------------------------------
</script>
